
namespace Parser
{
    /// <summary>
    /// Simple parser
    /// </summary>
    public class SimpleParser
    {
        /// <summary>
        /// Whole text (Read Only)
        /// </summary>
        public string String4Parsing { get; private set; }
        /// <summary>
        /// Current position (Read Only)
        /// </summary>
        public int Position {get; private set;}
        /// <summary>
        /// Whole string length (Read Only)
        /// </summary>
        public int Length { get; private set; }
        /// <summary>
        /// Last word finded by parser ("Skip" fill this field too)
        /// </summary>
        public string LastParsedWord { get; private set; }
        /// <summary>
        /// Let the string for parse
        /// </summary>
        /// <param name="par">Whole text for parsing</param>
        public SimpleParser(string par)
        {
            String4Parsing = par.Trim();
            Position = 0;
            Length = String4Parsing.Length;
        }

        /// <summary>
        /// Get the next word. Move position to the next word after first.
        /// </summary>
        /// <param name="HowMuchSkip">How much words skips before end</param>
        /// <returns></returns>
        public string GetWord(int HowMuchSkip = 0)
        {
            if (Position >= String4Parsing.Length)
                LastParsedWord = "";
            string word="";
            for(int count=0;count<=HowMuchSkip;count++)
            {
                word = "";
                for (; String4Parsing.Substring(Position, 1).Trim().Length > 0; )
                {
                    word += String4Parsing.Substring(Position, 1);
                    Position++;
                    if (Position >= String4Parsing.Length)
                    {
                        LastParsedWord = word;
                        return LastParsedWord;
                    }
                }
                for (; String4Parsing.Substring(Position, 1).Trim().Length == 0; )
                {
                    Position++;
                    if (Position >= String4Parsing.Length)
                    {
                        LastParsedWord = word;
                        return LastParsedWord;
                    }
                }
            }
            LastParsedWord = word;
            return LastParsedWord;
        }

        /// <summary>
        /// Skips the words
        /// </summary>
        /// <param name="HowMuchSkip">How much words skips</param>
        public void Skip(int HowMuchSkip)
        {
            GetWord(HowMuchSkip - 1);
        }

        /// <summary>
        /// Skips the words until it not equal to parameter or end of text
        /// </summary>
        /// <param name="What">What we search</param>
        /// <returns></returns>
        public bool SearchFor(string What)
        {
            string word="";
            for (; word != What; )
            {
                word = GetWord();
                if (Position >= String4Parsing.Length)
                    return false;
            }
            return true;
        }

    }
}
